import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/login",
    name: "login",
    meta: {
      title: '登录',
      requiresAuth: false, // false表示不需要登录
    },
    component: () => import('../views/LoginPage.vue'),
  },
  {
    path: "/register",
    name: "register",
    meta: {
      title: '注册',
      requiresAuth: false, // false表示不需要登录
    },
    component: () => import('../views/RegisterPage.vue'),
  },
  {
    path: '/resetpwd',
    name: 'ResetPassword',
    component: () => import('../views/ResetPassword.vue'),
  },
  {
    path: "/usercenter",
    name: "usercenter",
    meta: {
      requiresAuth: false, // false表示不需要登录
    },
    component: () => import("../views/UserCenter.vue"),
    children:[
        {
          path: "userinfo",
          name: "userinfo",
          meta: {
            title: '个人信息',
            requiresAuth: false, // false表示不需要登录
          },
          component: () => import("../components/UserInfo.vue"),
        },
        {
          path: "usersecurity",
          name: "usersecurity",
          meta: {
            title: '账号安全',
            requiresAuth: false, // false表示不需要登录
          },
          component: () => import("../components/UserSecurity.vue"),
        },
        {
          path: "scholarmanage",
          name: "scholarmanage",
          meta: {
            title: '门户管理',
            requiresAuth: false, // false表示不需要登录
          },
          component: () => import("../components/ScholarManage.vue"),
        },
        {
          path: "collection",
          name: "collection",
          meta: {
            title: '收藏列表',
            requiresAuth: false, // false表示不需要登录
          },
          component: () => import("../components/Collection.vue"),
        },
        {
          path: "history",
          name: "history",
          meta: {
            title: '浏览历史',
            requiresAuth: false, // false表示不需要登录
          },
          component: () => import("../components/History.vue"),
        },
        {
          path: "messagecenter",
          name: "messagecenter",
          meta: {
            title: '消息中心',
            requiresAuth: false, // false表示不需要登录
          },
          component: () => import("../components/MessageCenter.vue"),
          children:[
            {
              path: "privatemessage",
              name: "privatemessage",
              meta: {
                title: '私信',
                requiresAuth: false, // false表示不需要登录
              },
              component: () => import("../components/PrivateMessage.vue"),
            },
          ]
        },
        {
          path: "checkfollow",
          name: "checkfollow",
          meta: {
            title: '关注学者',
            requiresAuth: false, // false表示不需要登录
          },
          component: () => import("../components/CheckFollow.vue"),
        },
        {
          path: "pendingreview",
          name: "pendingreview",
          meta: {
            title: '审核事项',
            requiresAuth: false, // false表示不需要登录
          },
          component: () => import("../components/PendingReview.vue"),
        },
    ]
  },
  {
    path: '/userprofile/:uid',
    name: 'userprofile',
    meta: {
      requiresAuth: false, // false表示不需要登录
    },
    component: () => import('../views/OtherUserProfile.vue'),
  },
  {
    path: "/advsearch",
    name: "advanced search",
    meta: {
      title: '高级检索',
    },
    component: () => import("../components/advancedSearch.vue"),
  },
  {
    path: "/",
    name: "home",
    meta: {
      title: '首页', 
    },
    component: () => import("../components/firstPage.vue"),
  },
  {
    path: '/searchresult',
    name: 'SearchResult',
    meta: {
      title: '搜索结果',
    },
    component: () => import('../views/SearchResult.vue'),
  },
  {
    path: '/paperInfo/:pid',
    name: 'paperInfo',
    meta: {
      title: '成果详情',
    },
    component:() =>import('@/components/paperInfo.vue')
  },  
  {
      path: "/scholar",
      name: "scholar",
      meta: {
        title: '学者库',
      },
      component: () => import("../views/ScholarView.vue"),
  },
  {
    path: "/scholarresult",
    name: "scholarresult",
    component: () => import("../views/ScholarResult.vue"),
  },
    {
      path: "/scholarprofile/:sid",
      name: "scholarprofile",
      meta: {
        title: '学者门户',
      },
      component: () => import("../views/ScholarProfile.vue"),
    },
    {
      path: "/scholarnotfound",
      name: "scholarprofile",
      meta: {
        title: '学者门户不存在',
      },
      component: () => import("../components/ScholarNotFound.vue"),
    },
    {
      path: "/scholarprofile/:sid/claim",
      name: "claim",
      meta: {
            title: '认领',
      },
      component: () => import("../views/ScholarClaim.vue"),
    },
    // {
    //     path: "notices",
    //     name: "notices",
    //     meta: {
    //         requiresAuth: true, // false表示不需要登录
    //     },
    //     component: () => import("../components/NoticeView.vue"),
    //     children:[
    //         {
    //             path: "at",
    //             name: "at",
    //             meta: {
    //                 title: '私信',
    //               },
    //               component: () => import("../components/notices/At.vue"),
    //         },
    //         {
    //             path: "ask",
    //             name: "ask",
    //             meta: {
    //                 title: '追问追答',
    //               },
    //               component: () => import("../components/notices/Ask.vue"),
    //         },
    //         {
    //             path: "appeal",
    //             name: "appeal",
    //             meta: {
    //                 title: '申诉',
    //               },
    //               component: () => import("../components/notices/Appeal.vue"),
    //         },
    //     ]
    // },
      //   {
      //       path: "/paper",
      //       name: "paper",
      //       component: () => import("../components/PaperView.vue"),
      //       children:[
      //           {
      //               path: "claim",
      //               name: "clalm",
      //               meta: {
      //                   title: '文献申诉',
      //                 },
      //                 component: () => import("../components/PaperClaim.vue"),
      //           },
      //       ]
      //   },
      //   {
      //       path: "/classify",
      //       name: "classify",
      //         component: () => import("../components/AllClassifyView.vue"),
      //       children:[
      //           {
      //               path: ":cid",
                    
      //               meta: {
      //                   title: '分类显示页面',
      //                 },
      //                 component: () => import("../components/ClassifyView.vue"),
      //           },
      //       ]
      // }
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    //判断是否有标题
    document.title = to.meta.title;
  }
  // console.log("title",document.title)
  // 通过requiresAuth判断当前路由导航是否需要登录
  if (to.matched.some(record => record.meta.requiresAuth))
  {
    let storeState = JSON.parse(localStorage.getItem('storeState'));
    // 若需要登录访问，检查是否为登录状态
    if (!storeState)
    {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    }
    else
      next()
  }
  else
    next()// 确保一定要调用 next()
})

export default router;
